package com.web.netalbum.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.web.netalbum.bean.User;
import com.web.netalbum.dao.UserDao;
import com.web.netalbum.util.DBUtil;

public class UserDaoImpl implements UserDao {
	private QueryRunner qr=new QueryRunner(DBUtil.getDataSource());
	@Override
	public User findUserByUserName(String name) {
		try {
			return qr.query("select * from user where name=?", new BeanHandler<User>(User.class),name);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("用户不存在");
			e.printStackTrace();
		}	
		
		return null;
	}

	@Override
	public void save(User user) {
		try {
			qr.update("insert into user(id,name,password,email,actived,code)values(?,?,?,?,?,?)",user.getId(),user.getName(),user.getPassword(),user.getEmail(),user.isActived(),user.getCode());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	@Override
	public User findUserByUserNameAndPassword(String name, String password) {
		// TODO Auto-generated method stub
		try {
			return qr.query("select * from user where name=? and password=? ", new BeanHandler<User>(User.class),name,password);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public User findUserById(String id) {
		try {
			return qr.query("select *from user where id=?", new BeanHandler<User>(User.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

	@Override
	public void updateUser(User user) {
		try {
			qr.update("update user set password=?,actived=?,code=? where id=?", user.getPassword(),user.isActived(),user.getCode(),user.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public User findUserByEmail(String email) {
		
		try {
			return qr.query("select *from user where email=?", new BeanHandler<User>(User.class),email);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
